# Process RAW SPF DATA 
rm(list=ls())

(WD <- getwd())
if (!is.null(WD)) setwd(WD)

library(haven)
library(AER)
library(sandwich)
library(lmtest)
library(pracma)
library(stargazer)
library(plm)
library(pracma)
library(DataCombine)
library(jtools)
library(plyr)
library(readxl)
library(foreign)

##### SPF PGDP DATA #############
data    = read_excel("SPFmicrodata.xlsx", sheet = "PGDP")

data$f1      = as.numeric(data$PGDP1) 
data$f2      = as.numeric(data$PGDP2) 
data$f5      = as.numeric(data$PGDP5)
data$f6      = as.numeric(data$PGDP6)
data$quarter = as.numeric(data$QUARTER)
data$year    = as.numeric(data$YEAR) 
data$id      = as.numeric(data$ID) 

data$f1_yoy = 100*(data$f5/data$f1-1)
data$f2_yoy = 100*(data$f6/data$f2-1)

data$quarter_h1 = data$quarter -1 
data$quarter_h1[data$quarter == 1] = 4

data$year_h1 = data$year + 1
data$year_h1[data$quarter == 1] = data$year[data$quarter == 1]

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1_yoy) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1_yoy) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2_yoy) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2_yoy) )

data_clean       = merge(data_f1, data_f2)

data_gdp        = read_excel("data_realizations.xlsx")
data_gdp        = subset(data_gdp, select = c(year,quarter, p_realz,lp) )

data_clean      = merge(data_clean, data_gdp)

data            = data_clean
data$fc_err     = data$p_realz-data$f1_yoy
data$fc_rev     = data$f1_yoy-data$f2_yoy 
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1970,]
data$f1         = data$f1_yoy
data$f2         = data$f2_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev))

save(data,file="us_spf_pgdp_ind.rda")

data  = ddply(data,.(qdate),transform, cons = mean(f2,na.rm = TRUE))
write.dta(data, "us_spf_pgdp_ind.dta")

clear()


##### SPF GDP DATA #############
data    = read_excel("SPFmicrodata.xlsx", sheet = "RGDP")

data$f1      = as.numeric(data$RGDP1) 
data$f2      = as.numeric(data$RGDP2) 
data$f5      = as.numeric(data$RGDP5)
data$f6      = as.numeric(data$RGDP6)
data$quarter = as.numeric(data$QUARTER)
data$year    = as.numeric(data$YEAR) 
data$id      = as.numeric(data$ID) 

data$f1_yoy = 100*(data$f5/data$f1-1)
data$f2_yoy = 100*(data$f6/data$f2-1)

data$quarter_h1 = data$quarter -1 
data$quarter_h1[data$quarter == 1] = 4

data$year_h1 = data$year + 1
data$year_h1[data$quarter == 1] = data$year[data$quarter == 1]

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1_yoy) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1_yoy) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2_yoy) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2_yoy) )

data_clean       = merge(data_f1, data_f2, all = "TRUE")

data_gdp        = read_excel("data_realizations.xlsx")
data_gdp        = subset(data_gdp, select = c(year,quarter, y_realz,ly,lynow))

data_clean      = merge(data_clean, data_gdp, all = "TRUE")

data            = data_clean
data$f1         = data$f1_yoy
data$f2         = data$f2_yoy
data$fc_err     = data$y_realz-data$f1_yoy
data$fc_rev     = data$f1_yoy-data$f2_yoy 
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1970,]
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,y_realz,fc_err,fc_rev))

save(data,file="us_spf_rgdp_ind.rda")
clear()

##### SPF CPI DATA #############
data    = read_excel("SPFmicrodata.xlsx", sheet = "CPI")

data$f1      = as.numeric(data$CPI1) 
data$f2      = as.numeric(data$CPI2) 
data$f3      = as.numeric(data$CPI3) 
data$f4      = as.numeric(data$CPI4) 
data$f5      = as.numeric(data$CPI5)
data$f6      = as.numeric(data$CPI6)
data$quarter = as.numeric(data$QUARTER)
data$year    = as.numeric(data$YEAR) 
data$id      = as.numeric(data$ID) 

data$f1_yoy = 100*((1+data$f2/400)*(1+data$f3/400)*(1+data$f4/400)*(1+data$f5/400)-1)
data$f2_yoy = 100*((1+data$f3/400)*(1+data$f4/400)*(1+data$f5/400)*(1+data$f6/400)-1)

data$quarter_h1 = data$quarter -1 
data$quarter_h1[data$quarter == 1] = 4

data$year_h1 = data$year + 1
data$year_h1[data$quarter == 1] = data$year[data$quarter == 1]

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1_yoy) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1_yoy) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2_yoy) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2_yoy) )

data_clean       = merge(data_f1, data_f2)

data_gdp        = read_excel("data_realizations.xlsx")
data_gdp        = subset(data_gdp, select = c(year,quarter, pi_realz,flpi) )

data_clean      = merge(data_clean, data_gdp)

data            = data_clean
data$fc_err     = data$pi_realz-data$f1_yoy
data$fc_rev     = data$f1_yoy-data$f2_yoy 
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1980,]
data$f1         = data$f1_yoy
data$f2         = data$f2_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,pi_realz,fc_err,fc_rev))

save(data,file="us_spf_cpi_ind.rda")
clear()



##### SPF PGDP FINANCE DATA #############
data    = read_excel("SPFmicrodata.xlsx", sheet = "PGDP")

data    = data[data$INDUSTRY==1,]

data$f1      = as.numeric(data$PGDP1) 
data$f2      = as.numeric(data$PGDP2) 
data$f5      = as.numeric(data$PGDP5)
data$f6      = as.numeric(data$PGDP6)
data$quarter = as.numeric(data$QUARTER)
data$year    = as.numeric(data$YEAR) 
data$id      = as.numeric(data$ID) 

data$f1_yoy = 100*(data$f5/data$f1-1)
data$f2_yoy = 100*(data$f6/data$f2-1)

data$quarter_h1 = data$quarter -1 
data$quarter_h1[data$quarter == 1] = 4

data$year_h1 = data$year + 1
data$year_h1[data$quarter == 1] = data$year[data$quarter == 1]

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1_yoy) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1_yoy) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2_yoy) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2_yoy) )

data_clean       = merge(data_f1, data_f2)

data_gdp        = read_excel("data_realizations.xlsx")
data_gdp        = subset(data_gdp, select = c(year,quarter, p_realz,lp) )

data_clean      = merge(data_clean, data_gdp)

data            = data_clean
data$fc_err     = data$p_realz-data$f1_yoy
data$fc_rev     = data$f1_yoy-data$f2_yoy 
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data$f1         = data$f1_yoy
data$f2         = data$f2_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev))

save(data,file="us_spf_pgdp_ind_fin.rda")
clear()


##### SPF PGDP NON FINANCE DATA #############
data    = read_excel("SPFmicrodata.xlsx", sheet = "PGDP")

data    = data[data$INDUSTRY==2,]

data$f1      = as.numeric(data$PGDP1) 
data$f2      = as.numeric(data$PGDP2) 
data$f5      = as.numeric(data$PGDP5)
data$f6      = as.numeric(data$PGDP6)
data$quarter = as.numeric(data$QUARTER)
data$year    = as.numeric(data$YEAR) 
data$id      = as.numeric(data$ID) 

data$f1_yoy = 100*(data$f5/data$f1-1)
data$f2_yoy = 100*(data$f6/data$f2-1)

data$quarter_h1 = data$quarter -1 
data$quarter_h1[data$quarter == 1] = 4

data$year_h1 = data$year + 1
data$year_h1[data$quarter == 1] = data$year[data$quarter == 1]

data$quarter_h2 = data$quarter
data$year_h2    = data$year + 1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1_yoy) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1_yoy) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2_yoy) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2_yoy) )

data_clean       = merge(data_f1, data_f2)

data_gdp        = read_excel("data_realizations.xlsx")
data_gdp        = subset(data_gdp, select = c(year,quarter, p_realz,lp) )

data_clean      = merge(data_clean, data_gdp)

data            = data_clean
data$fc_err     = data$p_realz-data$f1_yoy
data$fc_rev     = data$f1_yoy-data$f2_yoy 
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data$f1         = data$f1_yoy
data$f2         = data$f2_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev))

save(data,file="us_spf_pgdp_ind_nonfin.rda")
clear()


##### SPF PGDP H=2 DATA #############
data    = read_excel("SPFmicrodata.xlsx", sheet = "PGDP")

data$f1      = as.numeric(data$PGDP1) 
data$f2      = as.numeric(data$PGDP2) 
data$f3      = as.numeric(data$PGDP3)
data$f4      = as.numeric(data$PGDP4)
data$quarter = as.numeric(data$QUARTER)
data$year    = as.numeric(data$YEAR) 
data$id      = as.numeric(data$ID) 

data$f1_yoy = 200*(data$f3/data$f1-1)
data$f2_yoy = 200*(data$f4/data$f2-1)

data$quarter_h1 = data$quarter + 1 
data$quarter_h1[data$quarter == 4] = 1

data$year_h1 = data$year 
data$year_h1[data$quarter == 4] = data$year[data$quarter == 4] + 1

data$quarter_h2 = data$quarter + 2
data$quarter_h2[data$quarter == 3] = 1
data$quarter_h2[data$quarter == 4] = 2

data$year_h2    = data$year 
data$year_h2[data$quarter == 3] = data$year[data$quarter == 3] + 1
data$year_h2[data$quarter == 4] = data$year[data$quarter == 4] + 1

data_f1          = subset(data, select = c(year_h1,quarter_h1,id,f1_yoy) )
data_f1$year     = data_f1$year_h1
data_f1$quarter  = data_f1$quarter_h1
data_f1          = subset(data_f1, select = c(year,quarter,id,f1_yoy) )

data_f2          = subset(data, select = c(year_h2,quarter_h2,id,f2_yoy) )
data_f2$year     = data_f2$year_h2
data_f2$quarter  = data_f2$quarter_h2
data_f2          = subset(data_f2, select = c(year,quarter,id,f2_yoy) )

data_clean       = merge(data_f1, data_f2)

data_gdp        = read_excel("data_realizations.xlsx")
data_gdp        = subset(data_gdp, select = c(year,quarter, p_realz,lp) )

data_clean      = merge(data_clean, data_gdp)

data            = data_clean
data$fc_err     = data$p_realz-data$f1_yoy
data$fc_rev     = data$f1_yoy-data$f2_yoy 
data$ID         = data$id
data$qdate      = data$year + data$quarter/4-0.25
data            = data[data$year>=1970,]
data$f1         = data$f1_yoy
data$f2         = data$f2_yoy
data            = subset(data, select = c(year,quarter,qdate,id,f1,f2,p_realz,fc_err,fc_rev))

save(data,file="us_spf_pgdp_ind_h2.rda")

clear()


